package com.dreamstations.changshads.fragment;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.dreamstations.changshads.R;
import com.dreamstations.changshads.adapter.ImagePagerAdapter;
import com.dreamstations.changshads.util.DSRequest;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;

/**
 * 用于显示点击图片后显示的 fragment
 */
public class ArticleDetailPagerFragment extends Fragment {
    private static final String POSITION = "position";      //点击的图片的序号
    private static final String IMAGE_URLS = "imgUrls";     //点击的图片的网络URL
    private int position;                                   //点击的图片的序号
    private String[] urls;                                  //点击的图片的网络URL
    private List<PhotoView> photos;
    private PhotoViewAttacher[] attachers;                  //用于设定 PhotoView 是否支持缩放


    public static ArticleDetailPagerFragment newInstance(int position, String[] urls) {
        ArticleDetailPagerFragment fragment = new ArticleDetailPagerFragment();
        Bundle args = new Bundle();
        args.putInt("position", position);
        args.putStringArray("imgUrls", urls);
        fragment.setArguments(args);
        Log.d("pagerImageFrag", "初始化成功");
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            position = getArguments().getInt(POSITION);
            urls = getArguments().getStringArray(IMAGE_URLS);
            Log.d("onCreate", position + "");
        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Log.d("onCreateView", "start init view");
        View view = inflater.inflate(R.layout.layout_imgpager, container, false);
        ViewPager viewPager = (ViewPager) view.findViewById(R.id.detail_viewpager);
        initData();

        ImagePagerAdapter adapter = new ImagePagerAdapter(photos);

        Log.d("onCreateView", position + "");
        Log.d("current Item", "现在是" + position);
        viewPager.setAdapter(adapter);
        viewPager.setCurrentItem(position);
        return view;
    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        AppCompatActivity activity = (AppCompatActivity) context;
        activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
    }

    private void initData() {
        photos = new ArrayList<>(urls.length);
        attachers = new PhotoViewAttacher[urls.length];

        for (int i = 0; i < urls.length; i++) {
            photos.add(new PhotoView(getActivity()));
            Picasso.with(getActivity())
                    .load(DSRequest.SERVER_BASE1+Uri.parse(urls[i]))
                    .into(photos.get(i));
            attachers[i] = new PhotoViewAttacher(photos.get(i), true);
        }

    }


}
